<template>
  <div class="production-warehouse-in">
    <el-card class="header-card">
      <div class="page-header">
        <div class="header-left">
          <h2>
            <el-icon><Download /></el-icon>
            生产入库管理
          </h2>
          <p>管理生产完工产品的入库操作</p>
        </div>
        <div class="header-right">
          <el-button type="primary" @click="showAddDialog">
            <el-icon><Plus /></el-icon>
            新建入库单
          </el-button>
        </div>
      </div>
    </el-card>

    <!-- 搜索筛选 -->
    <el-card class="filter-card">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="入库单号">
          <el-input v-model="searchForm.inboundNo" placeholder="请输入入库单号" clearable />
        </el-form-item>
        <el-form-item label="工单号">
          <el-input v-model="searchForm.workOrderNo" placeholder="请输入工单号" clearable />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="待入库" value="pending" />
            <el-option label="已入库" value="completed" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            查询
          </el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <el-table :data="tableData" stripe border v-loading="loading">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="inboundNo" label="入库单号" width="180" fixed />
        <el-table-column prop="workOrderNo" label="工单号" width="160" />
        <el-table-column prop="productName" label="产品名称" width="200" />
        <el-table-column prop="productCode" label="产品编码" width="150" />
        <el-table-column prop="quantity" label="入库数量" width="120" align="right">
          <template #default="{ row }">
            <el-tag type="success">{{ row.quantity }} {{ row.unit }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="qualifiedQty" label="合格数量" width="120" align="right" />
        <el-table-column prop="defectiveQty" label="不良数量" width="120" align="right">
          <template #default="{ row }">
            <span :style="{ color: row.defectiveQty > 0 ? '#f56c6c' : '#67c23a' }">
              {{ row.defectiveQty }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="warehouseName" label="入库仓库" width="120" />
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="row.status === 'completed' ? 'success' : 'warning'">
              {{ row.status === 'completed' ? '已入库' : '待入库' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="operator" label="操作员" width="100" />
        <el-table-column prop="createTime" label="创建时间" width="160" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" size="small" @click="handleView(row)">查看</el-button>
            <el-button link type="success" size="small" @click="handleConfirm(row)" v-if="row.status === 'pending'">确认入库</el-button>
            <el-button link type="danger" size="small" @click="handleDelete(row)" v-if="row.status === 'pending'">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
        class="pagination"
      />
    </el-card>

    <!-- 新建对话框 -->
    <el-dialog v-model="dialogVisible" title="新建入库单" width="800px">
      <el-form :model="formData" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="入库单号">
              <el-input v-model="formData.inboundNo" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工单号">
              <el-select v-model="formData.workOrderNo" placeholder="请选择工单" filterable>
                <el-option label="WO202501001" value="WO202501001" />
                <el-option label="WO202501002" value="WO202501002" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="产品名称">
              <el-select v-model="formData.productId" placeholder="请选择产品">
                <el-option label="产品A" value="1" />
                <el-option label="产品B" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库仓库">
              <el-select v-model="formData.warehouseId" placeholder="请选择仓库">
                <el-option label="成品仓" value="1" />
                <el-option label="半成品仓" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="入库数量">
              <el-input-number v-model="formData.quantity" :min="1" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合格数量">
              <el-input-number v-model="formData.qualifiedQty" :min="0" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="不良数量">
              <el-input-number v-model="formData.defectiveQty" :min="0" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注">
          <el-input v-model="formData.remark" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Download, Plus, Search } from '@element-plus/icons-vue'

const searchForm = reactive({
  inboundNo: '',
  workOrderNo: '',
  status: ''
})

const tableData = ref([])
const loading = ref(false)

const pagination = reactive({
  page: 1,
  size: 10,
  total: 0
})

const dialogVisible = ref(false)
const formData = reactive({
  inboundNo: '',
  workOrderNo: '',
  productId: '',
  warehouseId: '',
  quantity: 0,
  qualifiedQty: 0,
  defectiveQty: 0,
  remark: ''
})

const fetchData = () => {
  loading.value = true
  setTimeout(() => {
    tableData.value = [
      {
        id: '1',
        inboundNo: 'PI202501001',
        workOrderNo: 'WO202501001',
        productName: '产品A型号1',
        productCode: 'PA-001',
        quantity: 325,
        qualifiedQty: 320,
        defectiveQty: 5,
        unit: '件',
        warehouseName: '成品仓',
        status: 'completed',
        operator: '张三',
        createTime: '2025-01-10 16:00:00'
      },
      {
        id: '2',
        inboundNo: 'PI202501002',
        workOrderNo: 'WO202501002',
        productName: '产品B型号2',
        productCode: 'PB-002',
        quantity: 150,
        qualifiedQty: 150,
        defectiveQty: 0,
        unit: '件',
        warehouseName: '成品仓',
        status: 'pending',
        operator: '李四',
        createTime: '2025-01-11 14:00:00'
      }
    ]
    pagination.total = 2
    loading.value = false
  }, 500)
}

const handleSearch = () => {
  pagination.page = 1
  fetchData()
}

const handleReset = () => {
  Object.assign(searchForm, { inboundNo: '', workOrderNo: '', status: '' })
  handleSearch()
}

const handleSizeChange = (size) => {
  pagination.size = size
  fetchData()
}

const handlePageChange = (page) => {
  pagination.page = page
  fetchData()
}

const showAddDialog = () => {
  Object.assign(formData, {
    inboundNo: 'PI' + Date.now(),
    workOrderNo: '',
    productId: '',
    warehouseId: '',
    quantity: 0,
    qualifiedQty: 0,
    defectiveQty: 0,
    remark: ''
  })
  dialogVisible.value = true
}

const handleView = (row) => {
  ElMessage.info('查看入库单：' + row.inboundNo)
}

const handleConfirm = (row) => {
  ElMessageBox.confirm('确定完成入库操作吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('入库成功')
    fetchData()
  })
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确定删除该入库单吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
    fetchData()
  })
}

const handleSubmit = () => {
  ElMessage.success('保存成功')
  dialogVisible.value = false
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped lang="scss">
.production-warehouse-in {
  padding: 20px;

  .header-card {
    margin-bottom: 20px;
    
    .page-header {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .header-left {
        h2 {
          display: flex;
          align-items: center;
          gap: 10px;
          margin: 0 0 10px 0;
          font-size: 24px;
          color: #303133;
        }

        p {
          margin: 0;
          color: #909399;
          font-size: 14px;
        }
      }
    }
  }

  .filter-card {
    margin-bottom: 20px;
  }

  .table-card {
    .pagination {
      margin-top: 20px;
      justify-content: flex-end;
    }
  }
}
</style>

